$(function() {
	layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider','form'], function(){
		 var laydate = layui.laydate //日期
		  ,laypage = layui.laypage //分页
		  ,layer = layui.layer //弹层
		  ,table = layui.table //表格
		  ,carousel = layui.carousel //轮播
		  ,upload = layui.upload //上传
		  ,element = layui.element //元素操作
		  ,slider = layui.slider //滑块
		  var form = layui.form;
          form.render();
	var T =	table.render({
	    elem: '#courseware'
	    ,height: 420
	    ,url: '/selectCourseware' //数据接口
	    ,title: '课件表'
	    ,page: true //开启分页
	    ,limit:5
	    ,limits:[5,10,15,20]
	    ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
	    ,totalRow: true //开启合计行
	    ,response : {
            statusCode : 200// 重新规定成功的状态码为 200，table 组件默认为 0
        }
	    ,parseData : function(res) { // res 即为原始返回的数据
            return {
                "count" : res.tol,
                "code" : res.status, // 解析接口状态
                "msg" : res.message, // 解析提示文本
                "data" : res.list
                // 解析数据列表
            };
        }
	    ,cols: [[ //表头
	      {type: 'checkbox', fixed: 'left'}
	      ,{field: 'coursewareId', title: '课件ID',align:'center'}
	      ,{field: 'coursewareName', title: '课件名字',align:'center'}
	      ,{field: 'courseName', title: '课程名字',align:'center'}
	      ,{fixed: 'right', title:'操作',align:'center', toolbar: '#barDemo2'}
	    ]]
	  });
		 
		 table.on('tool(courseware)',function(obj){
			 var data = obj.data;
				console.log(data);
			 if(obj.event=='del'){
				 layer.confirm('真的删除行吗', function(index){
					 $.ajax({
                         url:"deleteCourseWare",
                         type:"post",
                         data:{coursewareId:data.coursewareId},
                         dataType:"json",
                         success:function () {
                             obj.del();
                             layer.close(index);
                             layer.msg("删除成功");
                             T.reload();
                         },
                         error:function () {
                             layer.msg("error");
                             layer.close(index);
                         }
                     })

                 });
				 }else if(obj.event=='update'){
						layer.open({
							type : 1,
							skin : 'layui-layer-rim', // 加上边框
							area : [ '520px', '400px' ], // 宽高
							content : '<form class="layui-form layui-form-pane" action="" id="form1">\n'
									+  '  <br>\n'
									+ '  <div class="layui-form-item">\n'
									+ '    <label class="layui-form-label">课件ID</label>\n'
									+ '    <div class="layui-input-block">\n'
									+ '      <input type="text" id="coursewareId" lay-verify="title" autocomplete="off"  readonly disabled class="layui-input layui-disabled">\n'
									+ '    </div>\n'
									+ '  </div>\n'
									+ '  <div class="layui-form-item">\n'
									+ '    <label class="layui-form-label">课件名字</label>\n'
									+ '    <div class="layui-input-block">\n'
									+ '      <input type="text" id="coursewareName" lay-verify="title" autocomplete="off"   class="layui-input">\n'
									+ '    </div>\n'
									+ '  </div>\n'
									
									+  '  <div class="layui-form-item">\n' 
				                    +  '    <label class="layui-form-label">课程名称</label>\n' 
				                    +  '    <div class="layui-input-block">\n'
				                    +  '    <select id="courseName"></select>\n' 
				                    +  '  </div>\n'
				                    +  '  </div>\n'
									
									+  '<div ><button lay-submit type="button" class="layui-btn" style="margin-left: 160px;" id="btn12">提交</button>\n'
									+  '<button type="reset" class="layui-btn layui-btn-primary">重置</button></div>\n'
									+  '</form>'
						});
						$("#coursewareId").val(data.coursewareId);
						$("#coursewareName").val(data.coursewareName);
					
						 $.ajax({
					    		url:"/selectCourses",
					    		dataType: 'json',
					            type: 'post',
					            success: function (data) {
					            	var da = eval(data);
					            	console.log(da);
					            	$("#courseName").append("<option ></option>");
					            	for(var i=0;i<da.length;i++){
					            		$("#courseName").append("<option value=\""+da[i].courseId+"\">"+da[i].courseName+"</option>");
					            		var form = layui.form;
					            		form.render('select');
					            	}
					            	
					            },
					            error: function () {
					                layer.alert("读取失败");
					            }
					        })
				 }
			 $("body").on("click","#btn12",function(){
				 var coursewareName = $("#coursewareName").val();
				 var courseId = $("#courseName").val();
				 var coursewareId = $("#coursewareId").val();
				 if(coursewareName!=null && coursewareId!=null && coursewareId!='' && coursewareName!=''){
				 $.ajax({
			    		url:"/updateCourseWare",
			    		dataType: 'json',
			            type: 'post',
			            data:{coursewareName:coursewareName,courseId:courseId,coursewareId:coursewareId},
			            success: function () {
			            	layer.closeAll();
			            	T.reload();
			            	layer.msg("修改成功");
			            },
			            error: function () {
			                layer.msg("修改失败");
			            }
			        })
				 }else{
					 layer.msg("存在空值，请补齐提交");
				 }
			 })
			 
			 })
	 $("#add1").on("click",function(){
	        		layer.open({
	                    title:'添加课件',
	                    type:1,
	                    area: ['600px', '400px'],
	                    content:'<div style="overflow:visible !important;">\n'+
	                    	'<form class="layui-form layui-form-pane" id="upload_form">\n'+
	                        '<br>\n'+
	                        '<div class="layui-form-item">\n' +
	                        '    <label class="layui-form-label">课件Id</label>\n' +
	                        '    <div class="layui-input-block">\n' +
	                        '        <input type="text" name="coursewareId1" id="coursewareId" autocomplete="off" placeholder="课时Id" class="layui-input" >\n' +
	                        '    </div>\n' +
	                        '</div>\n' +
	                        '<div class="layui-form-item">\n' +
	                        '    <label class="layui-form-label">课件名字</label>\n' +
	                        '    <div class="layui-input-block">\n' +
	                        '        <input type="text" name="coursewareName1" id="coursewareName" autocomplete="off" placeholder="请填写课件名称" class="layui-input" >\n' +
	                        '    </div>\n' +
	                        '</div>\n' +
	                        
	                        '<div class="layui-form-item">\n' +
	                        '    <label class="layui-form-label">所属课程</label>\n' +
	                        '    <div class="layui-input-block">\n' +
	                        '        <select id="courseName" name="courseName1" lay-filter="courseName">\n' +
	                        '        </select>\n'+
	                        '    </div>\n' +
	                        '</div>\n' +
	                        
	                        '<div class="layui-form-item">\n' +
	                        '    <label class="layui-form-label">学时</label>\n' +
	                        '    <div class="layui-input-block">\n' +
	                        '        <input type="text" name="cwPeriod1" id="cwPeriod" autocomplete="off" placeholder="请填写课件学时" class="layui-input" >\n' +
	                        '    </div>\n' +
	                        '</div>\n' +
	                        
	                        '<div class="layui-form-item">\n' +
	                        '   <li style="width:230px;margin-left: auto;margin-right: auto">\n'+
							'     <button lay-submit lay-filter="add_cw" id="add_cw" class="layui-btn" ><i class="layui-icon layui-icon-upload"></i>选择课件</button>\n'+
							'     <button lay-submit lay-filter="Cance2" class="layui-btn layui-btn-danger" >取消</button>\n'+
							'   </li>\n'+
	                        '</div>\n' +
	                        '</form>\n'+
	                        '</div>'
	                })
	                $.ajax({
	                	url:"/selectCourses",
	                	dataType:'json',
	                	type:'post',
	                	success:function(res){
	                		console.log(res)
	                		var d = res.data;
	                		
	                		$("#courseName").append("<option >请选择课程名称</option>")
	                		for (var i = 0; i < res.length; i++) {
								$("#courseName").append("<option value="+res[i].courseId+">"+res[i].courseName+"</option>")
								console.log("......"+res[i].courseName+".."+res[i].courseId);
								form.render('select');
							}
	                	},error:function(){
	                		layer.msg("error");
	                	}
	                })
	        })  
	        form.on('submit(Cance2)',function(obj){
	        			layer.closeAll();
	        			return false;
	        		})
			form.on('submit(add_cw)',function(obj){
				alert("aaaaaaa");
				var formData =obj.field;
				var courseName = formData.courseName1;//课程Id
				var cwPeriod = formData.cwPeriod1;//学时
				var coursewareId=formData.coursewareId1;//课程id
				var coursewareName=formData.coursewareName1;//课程名字
				if(courseName !='' && cwPeriod != ''){
					layer.open({
						title:'上传课件',
	                    type:1,
	                    area: ['400px', '200px'],
	                    content:'<div class="layui-upload">\n' +
	                    '<br>\n'+
	                    '<button type="button" class="layui-btn layui-btn-normal" id="choose"><i class="layui-icon layui-icon-upload"></i>上传课件</button>\n'+
	                    '<video style="display:none;" controls="controls" id="video"></video>\n'+
	                    '<br>\n'+
	                    '<br>\n'+
	                    '	<button type="button"  class="layui-btn layui-btn-fluid" id="upload">开始上传</button>\n'+
	                    '</div>\n'
					})
					
					upload.render({
	                	elem:'#choose',
	                	url:'/upload_submit',
	                	method:'post',
	                	auto:false,
	                	bindAction:'#upload',
	                	accpet:'video',
	                	acceptMime:'video/*',
	                	exts:'mp4|rm|rmvb|mov|avi',
	                	number:1,
	                	data:{
	                		courseId:courseName,
	                		cwPeriod:cwPeriod,
	                		coursewareId:coursewareId,
	                		coursewareName:coursewareName
	                	},choose:function(obj){
	                		obj.preview(function(index,file,result){
	                			console.log(file);
	                			$("#upload_form>span").remove();
	                			$("#choose").after("<span>&nbsp;&nbsp;已选中文件：<strong>"+file.name+"["+file.size/1024/1024+"M]</strong></span>");
	                			//向video添加视频地址
	                			var url = URL.createObjectURL(file); 
	                			console.log("111111url："+url); 
	                			$("#video").attr("src",url);
	                		})
	                	},before:function(obj){
	                		layer.load();
	                	},done:function(res,index,upload){
	                		var duration = document.getElementById("video").duration;//视频时长
	                		
	                		console.log("时间："+duration);
	                		
	                	
	                		$.ajax({
	                			url:'/updateCtime',//更新时长
	                			dataType:'json',
	                			type:'post',
	                			data:{
	                				coursewareId:res.coursewareId,
	                				Ctime:duration
	                			},success:function(){
	                				
	                				layer.closeAll();
	                				layer.msg("上传成功");
	                			},error:function(){
	                				layer.closeAll('loading');
	                				layer.msg("error2");
	                			}
	                		})
	                		
	                	},error:function(index,upload){
	                		layer.closeAll('loading');
	                		layer.msg('error1');
	                	}
	                })
			
				}else{
					layer.msg('请填写完整');
				}
				return false;
			})
    
	})
})